package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    /**
     * 添加规格参数
     *
     * @param specParamDTO 传入的而规格参数信息
     */
    @Override
    public void addSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        save(specParam);
    }

    /**
     * 根据条件查询所有的规格参数信息
     *
     * @param categoryId 商品分类id
     * @param groupId    规格组id
     * @param searching  是否搜索
     * @return
     */
    @Override
    public List<SpecParamDTO> querySpecParam(Long categoryId, Long groupId, Boolean searching) {
        List<SpecParam> specParams = list(new QueryWrapper<SpecParam>()
                // 用于判断当前categoryId是否有值
                .eq(null != categoryId, "category_id", categoryId)
                // 用于判断当前groupId是否有值
                .eq(null != groupId, "group_id", groupId)
                // 判断searching是否有值
                .eq(null != searching, "searching", searching));
        return SpecParamDTO.convertEntityList(specParams);
    }

    /**
     * 修改规格参数
     * @param specParamDTO 修改的参数信息
     */
    @Override
    public void updateSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        saveOrUpdate(specParam);
    }

    /**
     * 删除规格参数
     * @param pid 规格参数id
     */
    @Override
    public void deleteSpecParam(Long pid) {
        // TODO:规格参数直接删除合适嘛？
        removeById(pid);
    }

}